// pages/msgPage/msgPage.js
var app=getApp()
var openid=''
var selectid=''
var friend=[]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isme:'false',
    isfriend:'false',
    img:"",
    name:"",
    gender:"",
    campus:"",
    grade:"",
    hobby:"",
    array: {0:'未知',1:'男','2':'女','3':'保密'},
    array2: {0:'未知',1:'大学城校区',2:'五山校区'},
    array3: {0:'未知',1:'2020级',2:'2019级',3:'2018级',4:'2017级',5:"2016级"},
    myFriends:[]
  },

  toEdit(){
    wx.navigateTo({
      url: '../editInf/editInf',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.openid)
    openid=options.openid
    if(openid==app.globalData.openid)
          this.setData({
              isme:'true'
          })
    else
    this.setData({
      isme:'false'
  })
    this.load_data()


    // this.data.myFriends.splice(i-1,1)
    // this.setData({
    //   myFriends:this.data.myFriends,
    //   isfriend:this.data.isfriend
    // })


  },
  onPageScroll:function(){

  },

load_data(){
  let that=this
  //获取对方信息
  wx.cloud.callFunction({
    name:"getUserInfo",
    data:{
      openid:openid
    },
    success(res){
       that.setData({
         name:res.result.data['0']["name"],
         gender:res.result.data['0']["gender"],
         campus:res.result.data['0']["campus"],
         grade:res.result.data['0']["grade"],
         hobby:res.result.data['0']["hobby"],
         img:res.result.data[0]['img']
       })
    },
    fail(res){
     console.log("数据拉取失败",res)
    }
  })
   //获取自己的信息
   wx.cloud.callFunction({
    name:"getUserInfo",
    data:{
      openid:app.globalData.openid
    },
    success(res){
       console.log(res)
       that.setData({
         myFriends:res.result.data[0]['friend']
       })
       for(let k in that.data.myFriends)
       { 
         if(that.data.myFriends[k]['openid']==openid)
             {
                that.data.isfriend='true'
                break
             }
       }
       that.setData({
         isfriend:that.data.isfriend
       })
    },
    fail(res){
     console.log("数据拉取失败",res)
    }
  })

},

onShow: function () {
  this.load_data()
},

addfriend(){
  let that=this
  var ishas=true
  var obj
  wx.showLoading({
    title: '关注中...',
    mask:true
  })
  wx.cloud.database().collection("user_Info").where({openid:openid}).get().then(
    res=>{
      obj={
        'openid':res.data[0]['openid'],
        'name':res.data[0]['name'],
        'img':res.data[0]['img'],
        'hobby':res.data[0]['hobby']
      }

        wx.cloud.database().collection("user_Info").where({openid:app.globalData.openid}).get().then(
          res=>{
            that.data.myFriends=res.data[0]['friend']

            for(let index in that.data.myFriends)
            { 
              if(that.data.myFriends[index]['openid']==openid)
                  ishas=false
            }
            if(ishas)
               {that.data.myFriends.push(obj)
               }
            selectid=res.data[0]['_id']
          }).catch(err=>{console.log(err)})
     }).catch(err=>{console.log(err)})
     wx.hideLoading()
        wx.showToast({
          title: '关注成功！',
          icon: 'success',
          duration: 1500
        })
        this.setData({
          isfriend:'true'
        })
    
},
updateDB()
{
  let that=this
if(selectid!='')
 { 
  console.log(that.data.myFriends)
      wx.cloud.database().collection("user_Info").doc(selectid).update({
      data:{
            friend:that.data.myFriends
        },    
        success(res){
          console.log("成功",res)
        },
        fail(err){
          console.log("失败",err)
        }
      })
  }
},
onUnload: function () {
  this.updateDB()
  this.updateDB2()
},

delfriend(){
  wx.showLoading({
    title: '取消关注中...',
    mask:true
  })


  var i=0
  for(let k in this.data.myFriends)
  { if(k['openid']==openid)
         break
    i+=1
  }
  this.data.myFriends.splice(i-1,1)
  this.setData({
    myFriends:this.data.myFriends,
    isfriend:'false'
  })
  console.log(this.data.myFriends)
  wx.hideLoading()
        wx.showToast({
          title: '已取消关注',
          icon: 'success',
          duration: 1500
        })
},


updateDB2(){
  let that=this
  wx.cloud.database().collection('user_Info').where({
    openid:app.globalData.openid
  }).get({
    success(res){
      console.log(that.data.myFriends)
      wx.cloud.database().collection('user_Info').doc(res.data[0]['_id']).update({
        data:{
          friend:that.data.myFriends
        }
      })
    }
  })
}



})